<template>
  <div class="search">
    <h2>Search Github Users</h2>
    <input type="text" placeholder="enter the name you search" v-model="keyWord">
    <button @click="getUsers" @click.once="dontWelcome">Search</button>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name: 'SearchUsers',
  data() {
    return {
      keyWord: ''
    }
  },
  methods: {
    dontWelcome() {
      this.$bus.$emit('dontWelcome')
    },
    getUsers() {
      if (this.keyWord.trim().length === 0) return
      this.$bus.$emit('updateUserList', { isLoading: true, errMsg: '', users: [] })
      axios.get(`https://api.github.com/search/users?q=${this.keyWord}`).then(
        response => {
          this.$bus.$emit('updateUserList', { isLoading: false, errMsg: '', users: response.data.items })
          this.keyWord = ''
        },
        error => {
          this.$bus.$emit('updateUserList', { isLoading: false, errMsg: error.message, users: [] })
        }
      )
    }
  },
}
</script>

<style lang="css">
.search {
  background-color: #ddd;
  padding: 50px 10px 20px;
  margin: 0 20px 20px;
  border-radius: 10px;
}

.search input {
  margin-right: 5px;
  outline: none;
}
</style>
